package pe.gastobien.app.layer.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pe.gastobien.app.domain.Category;
import pe.gastobien.app.domain.CategoryDetail;
import pe.gastobien.app.layer.service.CategoryDetailService;
import pe.gastobien.app.layer.service.CategoryService;

/**
 * Servlet implementation class CategoryDetailController
 */
@WebServlet(name = "getCategoryDetailByCategoryId", urlPatterns = {
		"/getCategoryDetailByCategoryId", "/addNewCategoryDetail",
		"/deleteCategoryDetail" })
public class CategoryDetailController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		// TODO Auto-generated method stub
		String alias = request.getServletPath();

		if (alias.equals("/addNewCategory")) {
			addNewCategory(request, response);
		}
		if (alias.equals("/addNewCategoryDetail")) {
			addNewCategoryDetail(request, response);
		}
		if (alias.equals("/deleteCategoryDetail")) {
			deleteCategoryDetail(request, response);
		}
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String alias = request.getServletPath();
		if (alias.equals("/getCategoryDetailByCategoryId")) {
			getCategoryDetailByCategoryId(request, response);
		}
	}

	private void addNewCategory(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String destino = "";
		try {
			CategoryService service = new CategoryService();
			Category category = new Category();
			service.save(category);
			destino = "ccc";

		} catch (Exception e) {
			request.setAttribute("error", e.getMessage());
			destino = "error.jsp";
		}

		RequestDispatcher rd = request.getRequestDispatcher(destino);
		rd.forward(request, response);
	}

	private void getCategoryDetailByCategoryId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String destino = "";
		try {
			Integer categoryId = Integer.parseInt(request
					.getParameter("categoryId"));
			CategoryDetailService service = new CategoryDetailService();
			List<CategoryDetail> lista = service
					.getCategoryDetailByCategoryId(categoryId);
			request.setAttribute("lista", lista);
			if (lista.isEmpty()) {
				request.setAttribute("mensaje", "Tabla esta vacia.");
			}
			destino = "categorydetail.jsp";
		} catch (Exception e) {
			request.setAttribute("error", e.getMessage());
			destino = "error.jsp";
		}
		RequestDispatcher rd = request.getRequestDispatcher(destino);
		rd.forward(request, response);
	}

	private void addNewCategoryDetail(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException

	{
		CategoryDetailService service = new CategoryDetailService();
		String Subcategoria = request.getParameter("Subcategoria");
		String Descripcion = request.getParameter("Descripcion");
		Integer categoryID = Integer.parseInt(request
				.getParameter("categoryId"));

		CategoryDetail CategoryDetail = new CategoryDetail();

		CategoryDetail.setCategoryDescriptionDetail(Descripcion);
		CategoryDetail.setCategoryID(categoryID);
		CategoryDetail.setCategoryNameDetail(Subcategoria);

		CategoryDetail.setBudgetDetail(0.0);
		CategoryDetail.setSpendDetail(0.0);
		CategoryDetail.setYearDetail(2013);
		CategoryDetail.setMonthDetail(0);
		CategoryDetail.setActive(true);

		service.AddCategoryDetail(CategoryDetail);
		RequestDispatcher rd = request
				.getRequestDispatcher("categorydetail.jsp");
		rd.forward(request, response);
	}

	private void deleteCategoryDetail(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		CategoryDetailService service = new CategoryDetailService();
		CategoryDetail CategoryDetail = new CategoryDetail();
		Integer detalleCategoriaId = Integer.parseInt(request
				.getParameter("categoryDetailId"));
		CategoryDetail.setDetalleCategoriaId(detalleCategoriaId);
		service.delete(CategoryDetail);

		RequestDispatcher rd = request
				.getRequestDispatcher("categorydetail.jsp");
		rd.forward(request, response);
	}
}
